package edu.sysu.demo.petstore.token;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.IncorrectClaimException;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MissingClaimException;
import java.security.PublicKey;
import java.util.Date;

public class JWTVerifier implements Verifier {

  @Override
  public boolean isValid(String subject, String token, PublicKey publicKey) {


    try {

      Jws<Claims> jws = Jwts.parser().requireSubject(subject).setSigningKey(publicKey)
          .parseClaimsJws(token);

      Date expiration = jws.getBody().getExpiration();
      return new Date().before(expiration);
    } catch (IncorrectClaimException | MissingClaimException ice) {
      return false;
    }
  }
}
